package com.fernfx.xingtan.contacts.ui;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.contacts.contract.AddFriendsContract;
import com.fernfx.xingtan.contacts.presenter.AddFriendsPresenter;
import com.fernfx.xingtan.user.AccountManager;
import com.fernfx.xingtan.utils.SoftInputUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by admin on 2018/8/7.
 * 添加好友
 */
public class AddFriendsActivity extends BaseActivity implements View.OnClickListener, View.OnKeyListener, AddFriendsContract.View {
    public static boolean isStartFlag = true;

    @BindView(R.id.search_friends_edit)
    EditText searchFriendsEdit;

    @BindView(R.id.search_no_find_tv)
    TextView searchNotFindTv;

    private AddFriendsContract.Presenter presenter = new AddFriendsPresenter<>();

    public static void start(Context context) {
        Intent intent = new Intent(context, AddFriendsActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        if (isStartFlag) {
            isStartFlag = false;
        }
    }

    @Override
    protected void initData() {
        presenter.init(this);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_chat_add_friends;
    }

    @Override
    protected void initListener() {
        searchFriendsEdit.setOnKeyListener(this);
    }

    @OnClick({R.id.cancel_tv, R.id.add_friends_clt})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel_tv:
                SoftInputUtil.hideSoftInput(this);
                finish();
                break;
            case R.id.add_friends_clt:
                SoftInputUtil.hideSoftInput(this);
                break;
        }
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_ENTER == keyCode && KeyEvent.ACTION_DOWN == event.getAction()) {
            //处理事件
            String inputText = trimEditText(searchFriendsEdit);
            if (!TextUtils.isEmpty(inputText)) {
                if (TextUtils.equals(inputText,AccountManager.getUserInfo().getObj().getMobile())) {
                    ToastUtil.showCentertoast("不能添加自己");
                    return true;
                }
                requestArgsMap.clear();
                requestArgsMap.put("identity", inputText);
                presenter.request(requestArgsMap);
            }
            return true;
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!isStartFlag) {
            isStartFlag = true;
        }
        if (null != presenter){
            presenter.detachView();
            presenter = null;
        }
    }

    @Override
    public BaseActivity getActivity() {
        return this;
    }

    @Override
    public void setPointVisibility(int visibility) {
        searchNotFindTv.setVisibility(visibility);
    }

    @Override
    public String getFriendsAccount() {
        return trimEditText(searchFriendsEdit);
    }
}
